package com.lesama.sort;

import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {1, 3, 5, 4, 57, 96, 6, 7};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
        int[] arr2 = {1, 3, 5, 7, 9};
        bubbleSort(arr2);
        System.out.println(Arrays.toString(arr2));
    }
    public static void bubbleSort(int[] arr) {
        int n = arr.length;
        boolean flag = false;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - 1 - i; j++) {
                if(arr[j] > arr[j + 1]) {
                    flag = true;
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
            if(!flag) {
                break;
            }
        }
    }
}
